﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace agilecards.Model
{
    public class CardCollection : INotifyPropertyChanged, ICardCollection
    {
        public CardCollection()
        {
            Cards = new ObservableCollection<Card>();
            Cards.CollectionChanged += new NotifyCollectionChangedEventHandler(Cards_CollectionChanged);
            Name = "new collection";
        }

        public string Name
        {
            get { return _name; }
            set
            {
                if (value == _name) return;

                _name = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Name"));
            }
        }

        public string Id
        {
            get { return _id; }
            set
            {
                if (value == _id) return;

                _id = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Id"));
            }
        }

        public ObservableCollection<Card> Cards { get; private set; }

        public int Sum
        {
            get { return _sum; }
            set
            {
                if (value == _sum) return;

                _sum = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Sum"));
            }
        }

        public void AddCard(Point position)
        {
            Card card = new Card() { Position = position, Title = "new card", Description = "description...", Points = 0 };
            card.PropertyChanged += new PropertyChangedEventHandler(card_PropertyChanged);
            Cards.Add(card);
        }

        void card_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            HandlePointsChangedForCard(e);
        }

        private void HandlePointsChangedForCard(PropertyChangedEventArgs e)
        {
            if (e.PropertyName != "Points") return;

            this.UpdateCardSum();
        }

        void Cards_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            UpdateCardSum();
        }

        private void UpdateCardSum()
        {
            int sum = CalculateCardSum();
            this.Sum = sum;
        }

        private int CalculateCardSum()
        {
            int sum = 0;
            foreach (Card card in Cards)
            {
                sum += card.Points;
            }
            return sum;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, e);
            }
        }

        string _name;
        string _id;
        int _sum;

    }
}
